<?php

if(!defined('ROOT_PATH')){
	include(NP_PATH.'public/404 Not Found.htm');
	exit();
}
class action{

	protected $tpl ;
	public function __Construct() {
		session_start();
	}
	public function extend($class){
		$classfile = ROOT_PATH.'extend/'.$class.'.class.php';

		if(file_exists($classfile)){
			include($classfile);
		}else{
			error($class.'.class.php is not exist!');
		}
		if (class_exists($class)) {
			$myclass = new $class();
			return $myclass;
		}else{
			error('ERROR: Class name - '.$class.' is not exist !');
		}

	}
	
	
	public function M($table){

		$classfile = ROOT_PATH.'Models/'.$table.'Model.class.php';
		$classname = $table.'Model';

		if(file_exists($classfile)){
			include($classfile);
		}else{
			error($table.'Model.class.php is not exist!');
		}
		if (class_exists($classname)) {
			$m = new $classname();
			$m->set_table_name($table);
			return $m;
		}else{
			error('ERROR: Class name - '.$classname.' is not exist !');
		}

	}

	public function query($sql){

		$c = new Model();
		$resource = $c->query($sql);
		return $resource;
	}

	public function assign($key,$value='Null'){
		$this->tpl->assign($key,$value);
		global $$key;
		$$key = $value;
	}

	public function template(){
		include_once ('Smarty.class.php');
		$this->tpl= new Smarty();
		$this->tpl->template_dir = TPL_PATH; //濡剝婢樼捄顖氱窞
		$this->tpl->compile_dir = CACHE_TPLC; //濡剝婢樼紓鏍槯鐠侯垰绶�
		$this->tpl->cache_dir  = CACHE_PATH; //缂傛挸鐡ㄧ捄顖氱窞
		$this->tpl->cache_lifetime = CACHE_LIFETIME; //缂傛挸鐡ㄩ弮鍫曟？
		$this->tpl->caching = CACHE_FLAG; //瀵拷閸氼垳绱︾�涳拷
		
		$this->tpl->left_delimiter = LEFT_DELIMITER; //濡剝婢樼拠顓♀枅瀹革附鐖ｇ粵锟�
		$this->tpl->right_delimiter = RIGHT_DELIMITER; //濡剝婢樼拠顓♀枅閸欒櫕鐖ｇ粵锟�
	}
	
	//page
	public function toPage($table,$get,$post,$num){
		$page = intval($_GET[$get]);

		//print_r($_POST);
		//echo $_POST[$post];
		if (isset($_POST[$post]) ){
			$page = intval($_POST[$post]);
			//print_r($_POST);
		}

		$objmsgs = $this->M($table);
		$page = $page <= 0 ? 1 : $page;
		$pagesize = $num;
		$count = $objmsgs->count();
		$pages = ceil($count['count']/$pagesize);
		$page = $page > $pages ? $pages : $page;
		$start = ($page-1)*$pagesize; //page size $num;
		$pre = $page - 1 > 0 ? $page - 1 : 1; 
		$next = $page + 1 > $pages ? $pages : $page + 1; 
		$result = $objmsgs->limit('','',' order by id desc',"limit $start,$num");
		return array('page'=>$page,
				  'pages'=>$pages,
				  'pre'=>$pre,
				  'next'=>$next,
				  'result'=>$result
			);
	}
	
	
	public function display($file='index'){
			$this->tpl->display($file.TPL_Exte);
	}
	
	public function yzm() {
		Header("Content-type: image/PNG");
		//session_start();
		//閻㈢喐鍨氭宀冪槈閻礁娴橀悧锟�
		
		$im = imagecreate(44,18);
		$back = ImageColorAllocate($im, 245,245,245);
		imagefill($im,0,0,$back); //閼冲本娅�
		
		srand((double)microtime()*1000000);
		//閻㈢喐鍨�4娴ｅ秵鏆熺�涳拷
		for($i=0;$i<4;$i++){
		$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
		$authnum=rand(1,9);
		$vcodes.=$authnum;
		imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
		}
		
		for($i=0;$i<100;$i++) //閸旂姴鍙嗛獮鍙夊鐠烇紕绀�
		{ 
		$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
		imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
		} 
		ImagePNG($im);
		ImageDestroy($im);
		
		$_SESSION['VCODE'] = md5($vcodes);
	}
	
	protected function definition(){
		//
	}
	
	protected  function error($msg = 'ERROR !', $url=''){
		if($url=='') $url = __DOMAIN__.APP_NAME.'/';
		
		echo "<script> alert('$msg');window.document.location.href ='$url';</script>";
		exit;
	}
	protected function success($msg = 'Success !' , $url=''){
		if($url=='') $url = __DOMAIN__.APP_NAME.'/';
		echo "<script> alert('$msg');window.document.location.href='$url';</script>";
		exit;
	}
	
	function jump($url=''){
		if($url=='') $url = __DOMAIN__.APP_NAME.'/';
		echo "<script>window.document.location.href='$url';</script>";
		exit;
	}
	
	
	function loadCtl($group , $ctl='index'){

		$classfile = ROOT_PATH.'Controllers/'.$group.'/'.$ctl.'Controller.class.php';
		$class = $ctl.'Controller';
		//echo $classfile;
		if(file_exists($classfile)){
			include($classfile);
		}else{
			error($class.'Controller.class.php is not exist!');
		}
		if (class_exists($class)) {
			$myctl = new $class();
			return $myctl;
		}else{
			error('ERROR: Class name -> '.$class.' is not exist !');
		}
	}
	
//Class end 
}


?>
